/**
 * // Definition for a Node.
 * function Node(val,children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number[][]}
 */
 var levelOrder = function(root) {
  if(!root) return [];
  let now = [root];
  let next = [];
  let ret = [];
  while(now.length !== 0) {
    let current = [];
    while(now.length !== 0) {
      let n = now.shift();
      current.push(n.val);
      if(n.children) next.splice(next.length, 0, ...n.children);
    }
    now = next;
    next = [];
    ret.push(current);
  }
  return ret;
};